from flask import Flask,url_for,redirect,render_template,Blueprint
import chartkick
import os
app = Flask(__name__)
#Load chartkick
ck = Blueprint('ck_page', __name__, static_folder=chartkick.js(), static_url_path='/static')
app.register_blueprint(ck, url_prefix='/ck')
app.jinja_env.add_extension("chartkick.ext.charts")
#Load configure file
app.config.from_object("cmd")

@app.route("/index")
@app.route("/")
def charts():
    count = 1
    datalist=[]
    while True:
        tmplist=[]
        CMD="CMD"+str(count)
        TYPE = "GRAPHTYPE"+str(count)
        if CMD in app.config.keys() or TYPE in app.config.keys():
            #Deal With Data
            result = os.popen(app.config[CMD])
            result = result.read()
            result = result.split("\n")[0:-1]
            data=[]
            for i in result:
                i = i.split()
                data.append(i)
            tmplist.append(app.config[TYPE])
            tmplist.append(data)
            count = count + 1
            datalist.append(tmplist)
        else:
            break
    if count == 1:
        return render_template("error.html")
    else:
        return render_template("charts.html",datalist=datalist)

#this is a custome a filter function,but don't use.
#this function you can't to see
def filter_type(type):
    if type == 1:
        return "line_chart"
    elif type == 2:
        return "pie_char"
    elif type == 3:
        return "column_chart"
    else:
        return "bar_chart"
app.jinja_env.filters['switchtype'] = filter_type

if __name__=="__main__":
    app.run(debug=True,host="0.0.0.0")
